using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ProjectCodeS.Models;

namespace ProjectCodeS.Controllers
{   
    public class TicketsController : Controller
    {
		private readonly IClientRepository clientRepository;
		private readonly ITicketRepository ticketRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public TicketsController() : this(new ClientRepository(), new TicketRepository())
        {
        }

        public TicketsController(IClientRepository clientRepository, ITicketRepository ticketRepository)
        {
			this.clientRepository = clientRepository;
			this.ticketRepository = ticketRepository;
        }

        //
        // GET: /Tickets/

        public ViewResult Index()
        {
            return View(ticketRepository.AllIncluding(ticket => ticket.Client, ticket => ticket.TicketItems));
        }

        //
        // GET: /Tickets/Details/5

        public ViewResult Details(int id)
        {
            return View(ticketRepository.Find(id));
        }

        //
        // GET: /Tickets/Create

        public ActionResult Create()
        {
			ViewBag.PossibleClients = clientRepository.All;
            return View();
        } 

        //
        // POST: /Tickets/Create

        [HttpPost]
        public ActionResult Create(Ticket ticket)
        {
            if (ModelState.IsValid) {
                ticketRepository.InsertOrUpdate(ticket);
                ticketRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleClients = clientRepository.All;
				return View();
			}
        }
        
        //
        // GET: /Tickets/Edit/5
 
        public ActionResult Edit(int id)
        {
			ViewBag.PossibleClients = clientRepository.All;
             return View(ticketRepository.Find(id));
        }

        //
        // POST: /Tickets/Edit/5

        [HttpPost]
        public ActionResult Edit(Ticket ticket)
        {
            if (ModelState.IsValid) {
                ticketRepository.InsertOrUpdate(ticket);
                ticketRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleClients = clientRepository.All;
				return View();
			}
        }

        //
        // GET: /Tickets/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View(ticketRepository.Find(id));
        }

        //
        // POST: /Tickets/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            ticketRepository.Delete(id);
            ticketRepository.Save();

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing) {
                clientRepository.Dispose();
                ticketRepository.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

